#include "KdTreeModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.kdtree_model.json";
}

KdTreeModel::KdTreeModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/kdtree_search.html");
}

KdTreeModel& KdTreeModel::get()
{
    static KdTreeModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void KdTreeModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    sampleSize_ = json["sampleSize"].toInt(sampleSize_);
    sampleMinPos_ = json["sampleMinPos"].toDouble(sampleMinPos_);
    sampleMaxPos_ = json["sampleMaxPos"].toDouble(sampleMaxPos_);

    searchPointX_ = json["searchPointX"].toDouble(searchPointX_);
    searchPointY_ = json["searchPointY"].toDouble(searchPointY_);
    searchPointZ_ = json["searchPointZ"].toDouble(searchPointZ_);
    searchMethod_ = json["searchMethod"].toInt(searchMethod_);
    k_ = json["k"].toInt(k_);
    searchRadius_ = json["searchRadius"].toDouble(searchRadius_);
}

QJsonObject KdTreeModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("sampleSize", QJsonValue(sampleSize_));
    json.insert("sampleMinPos", QJsonValue(sampleMinPos_));
    json.insert("sampleMaxPos", QJsonValue(sampleMaxPos_));

    json.insert("searchPointX", QJsonValue(searchPointX_));
    json.insert("searchPointY", QJsonValue(searchPointY_));
    json.insert("searchPointZ", QJsonValue(searchPointZ_));
    json.insert("searchMethod", QJsonValue(searchMethod_));
    json.insert("k", QJsonValue(k_));
    json.insert("searchRadius", QJsonValue(searchRadius_));

    return json;
}
